package com.taptech.doufu.ui.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.taptech.doufu.ui.fragment.DFMemberFragment;
import com.taptech.doufu.bean.novel.VipTopicBean;
import com.taptech.doufu.ui.view.topicview.VipMixTopicView;
import com.taptech.doufu.ui.view.topicview.VipPrivitegeView;
import com.taptech.doufu.ui.view.topicview.VipTopicImageView;
import java.util.ArrayList;
import java.util.List;

/**
 * 小说列表适配器
 */
public class DFMemberAdapter extends BaseAdapter {

    //会员领取阅读券：
    public static final int TYPE_MIX = 1;
    //轮播图：
    public static final int TYPE_BANNER = 10;
    //普通小说整本限免：
    public static final int TYPE_NOMAL_ALL_FREE= 4;
    //对话小说整本限免区：
    public static final int TYPE_CHAT_NOVEL_ALL_FREE = 5;
    //会员小说8折区：
    public static final int TYPE_NOMAL_NOVEL_DISCOUNT = 2;
    //会员漫画9折区：
    public static final int TYPE_NOMAL_COMMIC_DISCOUNT = 3;
    //特权说明：
    public static final int TYPE_PRIVILEGE = 100;

    public static final int TYPE_VERTICAL = 20 ; //竖向布局
    public static final int TYPE_HORIZONTAL = 21 ; //横向滑动



    //------------UI卡片类型----------------------
    public static final int ADAPTER_ITEM_TYPE_MIX = 0;  //会员免费阅读券
    public static final int ADAPTER_ITEM_TYPE_BANNER = 1;  //轮播图类型
    public static final int ADAPTER_ITEM_TYPE_PRIVILEGE = 2;  //特权类型
    public static final int ADAPTER_ITEM_TYPE_NOMAL_NOVEL_LIMITE = 3;  //小说限免类型
    public static final int ADAPTER_ITEM_TYPE_CHAT_NOVEL = 4;  //对话小说类型
    public static final int ADAPTER_ITEM_TYPE_NOMAL_NOVEL_DISCOUNT = 5;  //小说折扣类型
    public static final int ADAPTER_ITEM_TYPE_COMMIC_DISCOUNT = 6;  //漫画折扣类型
    public static final int ADAPTER_ITEM_TYPE_VERTICAL = 7;  //漫画折扣类型
    public static final int ADAPTER_ITEM_TYPE_HORIZONTAL = 8;  //漫画折扣类型
    //----------------------------------

    public static final int TYPE_ITEM_COUNT = 9;

    private Context context;
    private List<VipTopicBean> contentList = new ArrayList<>();


    public DFMemberAdapter(Context context, List<VipTopicBean> contentList) {
        this.context = context;
        if (contentList != null) {
            this.contentList = contentList;
        }
    }

    public void refrushList(List<VipTopicBean> contentList) {
        if (contentList != null) {
            this.contentList = contentList;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (contentList != null) {
            return contentList.size();
        }else{
            return 0;
        }
    }


    @Override
    public VipTopicBean getItem(int position) {
        if (contentList != null && contentList.size() > position) {
            return contentList.get(position);
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_ITEM_COUNT;
    }


    @Override
    public int getItemViewType(int position) {
        VipTopicBean model = getItem(position);
        if (model != null) {
            if (model.getModel() == TYPE_PRIVILEGE) {
                return ADAPTER_ITEM_TYPE_PRIVILEGE;
            } else if (model.getModel() == TYPE_BANNER) {
                return ADAPTER_ITEM_TYPE_BANNER;
            }else if(model.getModel() == TYPE_NOMAL_ALL_FREE){
                return ADAPTER_ITEM_TYPE_NOMAL_NOVEL_LIMITE;
            } else if (model.getModel() == TYPE_CHAT_NOVEL_ALL_FREE) {
                return ADAPTER_ITEM_TYPE_CHAT_NOVEL;
            } else if (model.getModel() == TYPE_NOMAL_NOVEL_DISCOUNT) {
                return ADAPTER_ITEM_TYPE_NOMAL_NOVEL_DISCOUNT;
            } else if (model.getModel() == TYPE_NOMAL_COMMIC_DISCOUNT) {
                return ADAPTER_ITEM_TYPE_COMMIC_DISCOUNT;
            } else if(model.getModel() == TYPE_VERTICAL){
                return ADAPTER_ITEM_TYPE_VERTICAL;
            }else if(model.getModel() == TYPE_HORIZONTAL){
                return ADAPTER_ITEM_TYPE_HORIZONTAL;
            }else{
                return ADAPTER_ITEM_TYPE_MIX;
            }
        }
        return ADAPTER_ITEM_TYPE_MIX;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        VipTopicBean model = getItem(position);
        model.setListPos(position);
        int type = getItemViewType(position);
        if (type == ADAPTER_ITEM_TYPE_BANNER) {
            if (convertView == null) {
                convertView = new VipTopicImageView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new VipTopicImageView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == ADAPTER_ITEM_TYPE_PRIVILEGE) {
            if (convertView == null) {
                convertView = new VipPrivitegeView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new VipPrivitegeView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else {
            convertView = getMixView(convertView, model);
        }
        return convertView;
    }


    private View getMixView(View convertView,VipTopicBean model) {
        if (convertView == null) {
            convertView = new VipMixTopicView(context, model,DFMemberFragment.HOME_MEMBER_SEX_TYPE);
            convertView.setTag((model.getTitle()));
        } else {
            if (convertView.getTag().toString().equals(model.getTitle())) {
            } else {
                convertView = new VipMixTopicView(context, model,DFMemberFragment.HOME_MEMBER_SEX_TYPE);
                convertView.setTag((model.getTitle()));
            }
        }
        return convertView;
    }

}
